// userPackage/pages/address/address.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    selectFlag: false

  },

  selectHandle(e) {
    var obj = e.currentTarget.dataset.obj;
    console.log(obj);
    if (this.data.selectFlag) {
      wx.setStorageSync('address', JSON.stringify(obj))
      wx.navigateBack()
    }
  },

  editHandle(e) {
    var obj = e.currentTarget.dataset.obj;
    console.log(obj);
    wx.navigateTo({
      url: '/userPackage/pages/addAddress/addAddress?obj=' + JSON.stringify(obj),
    })
  },

  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag,
      val = e.detail.value;
    this.setData({
      [str]: val,
    })
    console.log(this.data);
  },


  getAddressList() {
    wx.showLoading({
      title: '',
    })
    var url = app.globalData.url + "ScOrder/listAddress",
      that = this;
    app.wxRequest(url, {
        userId: app.globalData.userId,
      }, res => {
        console.log(res);
        if (res.flag) {
          var datas = res.data;
          console.log(datas);
          datas.forEach(ele => {
            console.log(ele);
            ele.address1 = ''
            ele.address2 = ele.addressDetail
            if (ele.addressDetail != null && ele.addressDetail.includes('--')) {
              ele.address1 = ele.addressDetail.split('--')[0]
              ele.address2 = ele.addressDetail.split('--')[1]
            }

          })
          that.setData({
            list: datas
          })
          console.log(this.data.list);
          wx.hideLoading()
          wx.stopPullDownRefresh()
        }
      },
      err => {
        console.log(err);
      })
  },
  addHandle() {
    wx.navigateTo({
      url: '/userPackage/pages/addAddress/addAddress',
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);

    if (options.flag) {
      this.setData({
        selectFlag: true
      })
    }


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getAddressList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getAddressList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})